const component = {
  /**
   * 组件的属性列表
   */
  properties: {
    ids: {
      type: Object,
      value: {}
    },
    address: {
      type: String,
      value: '',
      observer: function (newVal) {
        this.setData({
          province: newVal,
          city: '',
          county: ''
        })
      }
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    provinces: [],
    province: '北京市',
    citys: [],
    city: '北京市',
    countys: [],
    county: '东城区',
    value: [0, 0, 0],
    isShow: false
  },
  attached () {
    let provinces = wx.getStorageSync('cityData')
    provinces.sort((a, b) => {
      if (a['id'] - 0 < b['id'] - 0) {
        return -1
      }
      if (a['id'] - 0 > b['id'] - 0) {
        return 1
      }
      return 0
    })
    this.setData({
      provinces,
      citys: provinces[0]['list'],
      countys: provinces[0]['list'][0]['list']
    })
  },

  /**
   * 组件的方法列表
   */
  methods: {
    bindChange (e) {
      const val = e.detail.value
      this.setData({
        value: val,
        citys: this.data.provinces[val[0]]['list'].length > 0 ? this.data.provinces[val[0]]['list'] : [],
        countys: this.data.provinces[val[0]]['list'].length > 0 ? (this.data.provinces[val[0]]['list'][val[1]] ? this.data.provinces[val[0]]['list'][val[1]]['list'] : []) : ''
      })
    },
    closeModal () {
      this.setData({
        isShow: false
      })
    },
    togglePicker () {
      this.setData({
        isShow: !this.data.isShow
      })
    },
    cancel () {
      this.setData({
        isShow: false
      })
    },
    done () {
      const val = this.data.value
      this.setData({
        province: this.data.provinces[val[0]].name,
        city: this.data.provinces[val[0]]['list'].length > 0 ? this.data.provinces[val[0]]['list'][val[1]].name : '',
        county: this.data.provinces[val[0]]['list'].length > 0 ? (this.data.provinces[val[0]]['list'][val[1]]['list'][val[2]] ? this.data.provinces[val[0]]['list'][val[1]]['list'][val[2]].name : '') : '',
        pid: this.data.provinces[val[0]].id,
        cid: this.data.provinces[val[0]]['list'].length > 0 ? this.data.provinces[val[0]]['list'][val[1]].id : '',
        did: this.data.provinces[val[0]]['list'].length > 0 ? (this.data.provinces[val[0]]['list'][val[1]]['list'][val[2]] ? this.data.provinces[val[0]]['list'][val[1]]['list'][val[2]].id : '') : '',
        isShow: false
      })
      this.triggerEvent('selected', {
        province: this.data.pid,
        city: this.data.cid,
        district: this.data.did
      })
    }
  }
}

Component(component)
